<?php
defined('IN_YZMPHP') or exit('Access Denied');
yzm_base::load_model('content', 'index', 0);

class index{

	/**
	 * 首页
	 */
	public function init() {
        if($_SERVER['REQUEST_URI'] == "/z"){
            echo file_get_contents("http://m.qinxichi.com/index.php?m=mobile");exit();
        }

		$site = get_config();
		$seo_title = $site['site_name'];
		$keywords = $site['site_keyword'];
		$description = $site['site_description'];

        $article = D('article');
        $article_count = $article->where(['status'=>1])->total();

        $category = D('category')->where('parentid = 0 and catid != 46')->field('catid,catname,pclink')->select();
        $show_array = [];
        foreach($category as $key => $val){
            $show_array['cat'.($key + 1)] = array(
                'cid' => $_SERVER['HTTP_HOST']  .$val['pclink'],
            );
            $child = D('category')->where('FIND_IN_SET('.$val['catid'].',arrparentid)')->select();

            foreach ($child as $c_k => $c_v){
                $show_array['cat'.($key + 1)]['subcats']['subcat' . ($c_k + 1)] = array(
                    'cid' => $c_v['catid'],
                    'name' => $c_v['catname'],
                    'url' => $c_v['pclink']
                );
            }
            $show_array['cat'.($key + 1)]['name'] = $val['catname'];
        }
        //幻灯片
        $banner = json_encode(D('banner')->where('status=1 and typeid = 1') -> field("title descs,image img,url") -> select());

        //底部分类栏
        $show_array_json = json_encode($show_array);

		include template('index', 'index');
	}

	public function getrecommend(){
        header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求

        $result = array('errCode' => 0,'errno' => 0,'errMsg' => 'success');

        $article = D('article');
        $article_count = $article->where(['status'=>1])->total();

        $category = D('category')->where(['parentid' => 0,'display' => 1])->field('catid,catname tag,CONCAT("http://'.$_SERVER['HTTP_HOST'].'",pclink) url')->select();

        $next = $_GET['next'];
        $type = $_GET['type'];

        switch ($type){
            case '1'://24小时
                //$start = strtotime('-1 days');
                $start = strtotime('-365 days');
                $end = time();
                break;
            case '2'://本周
                //$start = strtotime('-1weekMonday');
                $start = strtotime('-365 days');
                $end = time();
                break;
        }

        $pagelimit = $next * 6;
        $new_next = $next + 1;
        $flag = false;
        foreach($category as $key => &$val){
            /*tip:分组查询 $val['data'] = $article->fetch_all($article->query("SELECT a.url eidEnc,a.catid as vote,a.title,a.thumb as img,a.updatetime,a.nickname as author FROM `yzm_article` a inner join yzm_category b on a.catid=b.catid where a.click = (select max(click) from yzm_article where catid=a.catid and updatetime BETWEEN ".$start." AND ".$end.")  and FIND_IN_SET(".$val['catid'].",b.arrparentid) ORDER BY a.updatetime Desc limit $pagelimit,3 "));*/
            $val['data'] = $article->fetch_all($article->query("SELECT a.url eidEnc,a.catid as vote,a.title,a.thumb as img,a.updatetime,a.nickname as author FROM `yzm_article` a inner join yzm_category b on a.catid=b.catid where FIND_IN_SET(".$val['catid'].",b.arrparentid) ORDER BY a.inputtime Desc limit $pagelimit,6 "));
            /*foreach ($val['data'] as $k => &$v){
                $v['img'] = 'http://www.qinxichi.com' . $v['img'];
            }*/
            /* if(empty($val['data'])){
                $flag = true;
                break;
            }*/

        }

        if($flag){
            $category = [];
            $new_next = 0;
        }

        $result['data'] = array(
            'next' => $new_next,
            'list' => $category
        );

	    return_json($result);
    }


	/**
	 * 栏目列表页
	 */
	public function lists() {
		$catid = isset($_GET['catid']) ? intval($_GET['catid']) : 0;
        $types = isset($_GET['type']) ? intval($_GET['type']) : 0;
		if(!$catid) showmsg(L('missing_parameter'),'stop');
		$catinfo = get_category($catid);
		if(!$catinfo) showmsg(L('category_not_existent'),'stop');
		extract($catinfo);

		//外部链接
		if($type == 2) showmsg(L('is_jump'), $pclink, 1);

		//SEO相关设置
		$site = get_config();
		$seo_title = $seo_title ? $seo_title : $catname.'_'.$site['site_name'];
		$keywords = $seo_keywords ? $seo_keywords : $site['site_keyword'];
		$description = $seo_description ? $seo_description : $site['site_description'];

		$template = $catid==$arrchildid ? $list_template : $category_template;

		//单页面
		if($type == 1){
			$r = D('page')->where(array('catid'=>$catid))->find();
			extract($r);
			$template = $category_template;
		}

		//如果没有设置search,则为静态分页URL规则
		if(!isset($_GET['s'])) define('LIST_URL', true);


		include template('index', $template);
	}


	/**
	 * 内容页
	 */
	public function show() {
		$catid = isset($_GET['catid']) ? intval($_GET['catid']) : 0;
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if(!$catid || !$id) showmsg(L('missing_parameter'),'stop');

		$category = get_category($catid);
		if(!$category) showmsg(L('category_not_existent'),'stop');
		$modelid = $category['modelid'];
		$template = $category['show_template'];

		$tablename = get_model($modelid);
		if(!$tablename)  showmsg(L('model_not_existent'),'stop');
		$db = D($tablename);
		$data = $db->where(array('id'=>$id))->find();
		if(!$data || $data['status'] != 1 || $data['catid'] != $catid) showmsg(L('content_not_existent'),'stop');
		extract($data);

		//会员组权限检测
		if($groupids_view) {
			$groupid = intval(get_cookie('_groupid'));
			if(!$groupid){
				showmsg(L('need_login'), url_referer(get_url()), 2);
			}
			if($groupid < $groupids_view) showmsg(L('insufficient_authority'), 'stop');
		}

		//阅读收费检测
		if($readpoint) content::check_readpoint($catid.'_'.$id, $readpoint, $paytype, $url);

		//SEO相关设置
		$site = get_config();

		//更新点击量
		$db->update('`click` = `click`+1', array('id' => $id));

		//内容分页
		if(strpos($content, '_yzm_content_page_') !== false){
			$content = content::content_page($content);
		}

		//内容关键字
		if(get_config('keyword_link')){
			$content = content::keyword_content($content);
		}

		//获取相同分类的上一篇/下一篇内容
		$pre = $db->field('title,url')->where(array('id<'=>$id , 'status'=>'1' , 'catid'=>$catid))->order('id DESC')->find();
		$next = $db->field('title,url')->where(array('id>'=>$id , 'status'=>'1', 'catid'=>$catid))->order('id ASC')->find();
		$pre = $pre ? '<a href="'.$pre['url'].'">'.$pre['title'].'</a>' : L('already_is_first');
		$next = $next ? '<a href="'.$next['url'].'">'.$next['title'].'</a>' : L('already_is_last');

		//方法/步骤
		$step_list = D('article_step')->where(['article_id' => $data['id']])->order('id Asc')->select();
		$step_img_flag = false;
		foreach ($step_list as $k => $v){
		    if(!empty($v['step_img'])){
		        $step_img_flag = true;
            }
        }

        $video_list = D('article')->where('video_keywords like "%'.$data['video_keywords'].'%"  ')->field('video playUrl,video_keywords,thumb,title')->order('click Desc')->select();
        $json_video = json_encode($video_list);


		//视频
        if($data['video']){
            $video_sum = count($video_list);
        }else{
            $video_sum = 0;
        }

        $article_avatar = D('article_avatar')->where(['nickname' => $data['nickname']])->find();

		include template('index', $template);
	}

	public function step(){

        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $type = isset($_GET['type']) ? intval($_GET['type']) : 0;

        $article = D("article")->where(['id' => $id])->find();
        $article_step = D("article_step")->where(['article_id' => $id])->order('id Asc')->select();

        extract($article);
        include template('index', 'show_step');
    }

}